package base.render.gui;

import java.awt.Point;
import java.awt.Rectangle;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;

import base.collision.Collision;
import base.common.IBoundingBox;
import base.render.Render;

public abstract class GuiButton extends Gui implements IBoundingBox
{
	protected Rectangle boundingBox;
	protected int colorDefault = 0x999999;
	protected int colorHover = 0xFFFFFF;
	protected int colorAction = 0x666666;
	protected int colorBackDefault = 0x454545;
	protected int renderColor = colorDefault;
	protected int renderBackColor = colorBackDefault;

	protected boolean isPressed = false;

	public GuiButton(int par1, int par2, int par3, int par4)
	{
		this.boundingBox = new Rectangle(par1, par2, par3, par4);
	}

	@Override
	public Rectangle getBoundingBox()
	{
		return this.boundingBox;
	}

	@Override
	public void onUpdate()
	{
		if (this.isPressed && !Mouse.isButtonDown(0))
		{
			this.isPressed = false;
			this.renderColor = this.colorDefault;
			if (Collision.isCollideWith(this, new Point(Mouse.getX(), Mouse.getY())))
			{
				//Pressed the button
				this.onAction();
			}
		}
		else if (Collision.isCollideWith(this, new Point(Mouse.getX(), Mouse.getY())))
		{
			if (Mouse.isButtonDown(0))
			{
				//Holding the button
				this.isPressed = true;
				this.renderColor = this.colorAction;
				this.renderBackColor = this.colorHover;
			}
			else
			{
				//Hovering over the button
				this.renderColor = this.colorHover;
				this.renderBackColor = this.colorBackDefault;
			}
		}
		else
		{
			this.renderColor = this.colorDefault;
			this.renderBackColor = this.colorBackDefault;
		}
	}

	/**Called when clicked on*/
	public abstract void onAction();

	@Override
	public void onRender()
	{
		GL11.glPushMatrix();
		Render.drawWithColor(this.renderBackColor);
		Render.drawBox(this.boundingBox.x, this.boundingBox.y, this.boundingBox.width+4, this.boundingBox.height+4);
		GL11.glPopMatrix();
		
		GL11.glPushMatrix();
		Render.drawWithColor(this.renderColor);
		Render.drawBox(this.boundingBox.x + 2, this.boundingBox.y + 2, this.boundingBox.width, this.boundingBox.height);
		GL11.glPopMatrix();
	}
}
